home *** CD-ROM | disk | FTP | other *** search
/ Creating Your Own America Online Web Pages / Creating Your Own America Online Web Pages.iso / TOOLS / TEX2RTF / SOURCES.ZIP / SRC / BMPUTILS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  5.7 KB  |  221 lines

  1. /*
  2.  * Utilities for manipulating bitmap and metafile images for
  3.  * the purposes of conversion to RTF
  4.  *
  5.  */
  6.  
  7. static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
  8.   'C', 'D', 'E', 'F' };
  9.  
  10. void DecToHex(int dec, char *buf)
  11. {
  12.   int firstDigit = (int)(dec/16.0);
  13.   int secondDigit = (int)(dec - (firstDigit*16.0));
  14.   buf[0] = hexArray[firstDigit];
  15.   buf[1] = hexArray[secondDigit];
  16.   buf[2] = 0;
  17. }
  18.  
  19. static unsigned int getshort(FILE *fp)
  20. {
  21.   int c, c1;
  22.   c = getc(fp);  c1 = getc(fp);
  23.   return ((unsigned int) c) + (((unsigned int) c1) << 8);
  24. }
  25.  
  26. static unsigned long getint(FILE *fp)
  27. {
  28.   int c, c1, c2, c3;
  29.   c = getc(fp);  c1 = getc(fp);  c2 = getc(fp);  c3 = getc(fp);
  30.   return (long)((long) c) +
  31.          (((long) c1) << 8) + 
  32.      (((long) c2) << 16) +
  33.      (((long) c3) << 24);
  34. }
  35.  
  36. Bool GetBMPHeader(FILE *fp, int *Width, int *Height, int *Planes, int *BitsPerPixel)
  37. {
  38.   unsigned long bfSize, bfOffBits, biSize, biWidth, biHeight, biPlanes;
  39.   unsigned long biBitCount, biCompression, biSizeImage, biXPelsPerMeter;
  40.   unsigned long biYPelsPerMeter, biClrUsed, biClrImportant;
  41.  
  42.   /* read the file type (first two bytes) */
  43.   int c = getc(fp); int c1 = getc(fp);
  44.   if (c!='B' || c1!='M') { return FALSE; }
  45.  
  46.   bfSize = getint(fp);
  47.   getshort(fp);         /* reserved and ignored */
  48.   getshort(fp);
  49.   bfOffBits = getint(fp);
  50.  
  51.   biSize          = getint(fp);
  52.   biWidth         = getint(fp);
  53.   biHeight        = getint(fp);
  54.   biPlanes        = getshort(fp);
  55.   biBitCount      = getshort(fp);
  56.   biCompression   = getint(fp);
  57.   biSizeImage     = getint(fp);
  58.   biXPelsPerMeter = getint(fp);
  59.   biYPelsPerMeter = getint(fp);
  60.   biClrUsed       = getint(fp);
  61.   biClrImportant  = getint(fp);
  62.  
  63.   *Width = (int)biWidth;
  64.   *Height = (int)biHeight;
  65.   *Planes = (int)biPlanes;
  66.   *BitsPerPixel = (int)biBitCount;
  67.  
  68. //  fseek(fp, bfOffBits, SEEK_SET);
  69.   
  70.   return TRUE;
  71. }
  72.  
  73. static int scanLineWidth = 0;
  74.  
  75. Bool OutputBitmapHeader(FILE *fd, Bool isWinHelp = FALSE)
  76. {
  77.   int Width, Height, Planes, BitsPerPixel;
  78.   if (!GetBMPHeader(fd, &Width, &Height, &Planes, &BitsPerPixel))
  79.     return FALSE;
  80.  
  81.   scanLineWidth = (int)(Width/(8/BitsPerPixel));
  82.   if ((float)((int)(scanLineWidth/2.0)) != (float)(scanLineWidth/2.0))
  83.     scanLineWidth ++;
  84.     
  85.   int goalW = 15*Width;
  86.   int goalH = 15*Height;
  87.  
  88.   TexOutput("{\\pict");
  89.   if (isWinHelp) TexOutput("\\wbitmap0");
  90.   else TexOutput("\\dibitmap");
  91.  
  92.   char buf[50];
  93.   TexOutput("\\picw"); sprintf(buf, "%d", Width); TexOutput(buf);
  94.   TexOutput("\\pich"); sprintf(buf, "%d", Height); TexOutput(buf);
  95.   TexOutput("\\wbmbitspixel"); sprintf(buf, "%d", BitsPerPixel); TexOutput(buf);
  96.   TexOutput("\\wbmplanes"); sprintf(buf, "%d", Planes); TexOutput(buf);
  97.   TexOutput("\\wbmwidthbytes"); sprintf(buf, "%d", scanLineWidth); TexOutput(buf);
  98.   TexOutput("\\picwgoal"); sprintf(buf, "%d", goalW); TexOutput(buf);
  99.   TexOutput("\\pichgoal"); sprintf(buf, "%d", goalH); TexOutput(buf);
  100.   TexOutput("\n");
  101.   return TRUE;
  102. }
  103.  
  104.  
  105. Bool OutputBitmapData(FILE *fd)
  106. {
  107.   fseek(fd, 14, SEEK_SET);
  108.   int bytesSoFar = 0;
  109.   int ch = getc(fd);
  110.   char hexBuf[3];
  111.   while (ch != EOF)
  112.   {
  113.     if (bytesSoFar == scanLineWidth)
  114.     {
  115.       bytesSoFar = 0;
  116.       TexOutput("\n");
  117.     }
  118.     DecToHex(ch, hexBuf);
  119.     TexOutput(hexBuf);
  120.     bytesSoFar ++;
  121.     ch = getc(fd);
  122.   }
  123.   TexOutput("\n}\n");
  124.   return TRUE;
  125. }
  126.  
  127. #ifdef wx_msw
  128. struct mfPLACEABLEHEADER {
  129.     DWORD    key;
  130.     HANDLE    hmf;
  131.     RECT    bbox;
  132.     WORD    inch;
  133.     DWORD    reserved;
  134.     WORD    checksum;
  135. };
  136.  
  137. // Returns size in TWIPS
  138. Bool GetMetafileHeader(FILE *handle, int *width, int *height)
  139. {
  140.   char buffer[40];
  141.   mfPLACEABLEHEADER *theHeader = (mfPLACEABLEHEADER *)&buffer;
  142.   fread((void *)theHeader, sizeof(char), sizeof(mfPLACEABLEHEADER), handle);
  143.   if (theHeader->key != 0x9AC6CDD7)
  144.   {
  145.     return FALSE;
  146.   }
  147.  
  148.   float widthInUnits = (float)theHeader->bbox.right - theHeader->bbox.left;
  149.   float heightInUnits = (float)theHeader->bbox.bottom - theHeader->bbox.top;
  150.   *width = (int)((widthInUnits*1440.0)/theHeader->inch);
  151.   *height = (int)((heightInUnits*1440.0)/theHeader->inch);
  152.   return TRUE;
  153. }
  154.  
  155. Bool OutputMetafileHeader(FILE *handle, Bool isWinHelp, int userWidth, int userHeight)
  156. {
  157.   int Width, Height;
  158.   if (!GetMetafileHeader(handle, &Width, &Height))
  159.     return FALSE;
  160.  
  161.   scanLineWidth = 64;
  162.   int goalW = Width;
  163.   int goalH = Height;
  164.  
  165.   // Scale to user's dimensions if we have the information
  166.   if (userWidth > 0 && userHeight == 0)
  167.   {
  168.     double scaleFactor = ((double)userWidth/(double)goalW);
  169.     goalW = userWidth;
  170.     goalH = (int)((goalH * scaleFactor) + 0.5);
  171.   }
  172.   else if (userWidth == 0 && userHeight > 0)
  173.   {
  174.     double scaleFactor = ((double)userHeight/(double)goalH);
  175.     goalH = userHeight;
  176.     goalW = (int)((goalW * scaleFactor) + 0.5);
  177.   }
  178.   else if (userWidth > 0 && userHeight > 0)
  179.   {
  180.     goalW = userWidth;
  181.     goalH = userHeight;
  182.   }
  183.  
  184.   TexOutput("{\\pict");
  185.   TexOutput("\\wmetafile8");
  186.  
  187.   char buf[50];
  188.   TexOutput("\\picw"); sprintf(buf, "%d", Width); TexOutput(buf);
  189.   TexOutput("\\pich"); sprintf(buf, "%d", Height); TexOutput(buf);
  190.   TexOutput("\\picwgoal"); sprintf(buf, "%d", goalW); TexOutput(buf);
  191.   TexOutput("\\pichgoal"); sprintf(buf, "%d", goalH); TexOutput(buf);
  192.   TexOutput("\n");
  193.   return TRUE;
  194. }
  195.  
  196. Bool OutputMetafileData(FILE *handle)
  197. {
  198.   int bytesSoFar = 0;
  199.   char hexBuf[3];
  200.   int ch;
  201.   do
  202.   {
  203.     ch = getc(handle);  
  204.     if (bytesSoFar == scanLineWidth)
  205.     {
  206.       bytesSoFar = 0;
  207.       TexOutput("\n");
  208.     }
  209.     if (ch != EOF)
  210.     {
  211.       DecToHex(ch, hexBuf);
  212.       TexOutput(hexBuf);
  213.       bytesSoFar ++;
  214.     }
  215.   } while (ch != EOF);
  216.   TexOutput("\n}\n");
  217.   return TRUE;
  218. }
  219.  
  220. #endif
  221.